﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ServiceStack.Redis;
using ServiceStack.Text;
using StackExchange.Redis;

namespace EMS.system.Common
{
    /// <summary>
    /// 添加Nuget引用：stackexchang.Redis
    /// RedisCluster缓存操作类
    /// </summary>
    public class RedisClusterHelper
    {
        private static IDatabase db = null;

        private static ConfigurationOptions ConfigOpt
        {
            get
            {
                ConfigurationOptions configOpt = new ConfigurationOptions();
                configOpt.EndPoints.Add("127.0.0.1", 6381);
                configOpt.EndPoints.Add("127.0.0.1", 6382);
                configOpt.EndPoints.Add("127.0.0.1", 6383);
                configOpt.EndPoints.Add("127.0.0.1", 6384);
                configOpt.EndPoints.Add("127.0.0.1", 6385);
                configOpt.EndPoints.Add("127.0.0.1", 6386);
                return configOpt;
            }
        }

        /// <summary>
        /// 定义缓存对象
        /// </summary>
        private static IDatabase RedisDB
        {
            get
            {
                if (db == null)
                {
                    ConnectionMultiplexer _redis = ConnectionMultiplexer.Connect(ConfigOpt);
                    db = _redis.GetDatabase();
                }
                return db;
            }
        }

        /// <summary>
        /// 判断某个数据是否已经被缓存
        /// </summary>
        public static bool Exist(string key)
        {
            try
            {
                var result = RedisDB.KeyExists(key);
                return result;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 存储数据到hash表
        /// </summary>
        public static bool Set(string key, string value)
        {
            try
            {
                var startTime = DateTime.Now;
                var endTime = DateTime.Now.AddHours(12).AddSeconds(new Random().Next(0, 99));
                TimeSpan ts = endTime.Subtract(startTime);

                var tmpVal = JsonSerializer.SerializeToString<string>(value);
                RedisDB.StringSet(key, tmpVal, ts);
            }
            catch
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 存储数据到hash表
        /// </summary>
        public static bool Set(string key, string value, DateTime tmpExpire)
        {
            try
            {
                var ts = tmpExpire.Subtract(DateTime.Now);
                var tmpVal = JsonSerializer.SerializeToString<string>(value);
                RedisDB.StringSet(key, tmpVal, ts);
            }
            catch
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 存储数据到hash表
        /// </summary>
        public static bool Set<T>(string key, T t)
        {
            try
            {
                var startTime = DateTime.Now;
                var endTime = DateTime.Now.AddHours(12).AddSeconds(new Random().Next(0, 99));
                TimeSpan ts = endTime.Subtract(startTime);
                var tmpVal = JsonSerializer.SerializeToString<T>(t);
                var result = RedisDB.StringSet(key, tmpVal, ts);
                return result;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 存储数据到hash表
        /// </summary>
        public static bool Set<T>(string key, T t, DateTime tmpExpire)
        {
            try
            {
                var ts = tmpExpire.Subtract(DateTime.Now);
                var tmpVal = JsonSerializer.SerializeToString<T>(t);
                RedisDB.StringSet(key, tmpVal, ts);
            }
            catch
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 移除key的value
        /// </summary>
        public static bool Remove(string key)
        {
            try
            {
                var result = RedisDB.KeyDelete(key);
                return result;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 根据key获取数据
        /// </summary>
        public static string Get(string key)
        {
            try
            {
                var value = RedisDB.StringGet(key);
                var result = JsonSerializer.DeserializeFromString<string>(value);
                return result;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 根据key获取数据
        /// </summary>
        public static T Get<T>(string key)
        {
            try
            {
                var value = RedisDB.StringGet(key);
                var result = JsonSerializer.DeserializeFromString<T>(value);
                return result;
            }
            catch
            {
                return default;
            }
        }
    }
}
